//
//  RegisterWindowController.m
//  HiQ
//
//  Created by arvintan on 12-7-28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "RegisterWindowController.h"



@interface RegisterWindowController ()

@end

@implementation RegisterWindowController

@synthesize _headImage;

@synthesize _textName;
@synthesize _textPassword;
@synthesize _textPasswordConfirm;
@synthesize _comboSex;
@synthesize _buttonRegister;
@synthesize _buttonCancel;
@synthesize _buttonHeadImage;


- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

-(void) awakeFromNib
{
    _headImage = [NSImage imageNamed:@"H1"];
    [_buttonHeadImage setImage:_headImage];
}

-(void) registerAlter:(NSString *)infoText MesageText:(NSString*) messageText
{
    NSAlert *alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:NSLocalizedString(@"确定", nil)];
    [alert setMessageText:NSLocalizedString(messageText,nil)];
    [alert setInformativeText:NSLocalizedString(infoText, nil)];
    [alert setAlertStyle:NSWarningAlertStyle];
    [alert beginSheetModalForWindow:[NSApp mainWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
}

-(void)didCloseAlter:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
    [self close];
}

#pragma mark -
#pragma mark button action


-(IBAction)onRegister:(id)sender
{
    //获取本地文件中server端的ip和port
    NSString *host = [[NSUserDefaults standardUserDefaults] stringForKey:@"SERVER_IP"];
    NSUInteger port = [[[NSUserDefaults standardUserDefaults] stringForKey:@"SERVER_PORT"] intValue];
    
    if ([[_textName stringValue] isEqualTo:@""]) 
    {
        [self registerAlter:NSLocalizedString(@"昵称不能为空",nil) MesageText:NSLocalizedString(@"昵称错误",nil)];
        return;
    }
    if ([[_textPassword stringValue] length] < 3 || [[_textPassword stringValue] length] > 15) {
        [self registerAlter:NSLocalizedString(@"密码长度必须在3-15位之间",nil) MesageText:NSLocalizedString(@"密码错误",nil)];
        return;
    }
    if (![[_textPassword stringValue] isEqualTo: [_textPasswordConfirm stringValue]]) 
    {
        [self registerAlter:NSLocalizedString(@"两次输入密码不符",nil) MesageText:NSLocalizedString(@"密码错误",nil)];
        return;
    }
    
    NSString *name = [_textName stringValue];
    NSString *password = [_textPassword stringValue];
    NSString *sex = [_comboSex stringValue];
    
    
    //    NSString *host = @"127.0.0.1";
    //    NSUInteger port = 9527;
    
    //设置注册logic类代理
    _register = [[RegisterUser alloc] initWithHost:host andport:(int)port];
    _register._delegate = self;
    
    
    [_register sendRegisterMsg:name Password:password HeadImage:[_headImage name] Sex:sex];
    
    //test 旧的sendMsg接口
    //    NSString *registerMsg = [[NSString alloc] initWithFormat:@"%@/%@/%@",_name,_password,_sex];
    //    [_register sendMsg:registerMsg];
}

-(void) registerTimeOut
{
    [self registerAlter:NSLocalizedString(@"注册超时",nil) MesageText:NSLocalizedString(@"网络问题",nil)];
}


-(IBAction)onCancel:(id)sender
{
    [self close];
}

-(IBAction)onHeadImage:(id)sender
{
    if (_switchHeadImageWindowController == nil)
		_switchHeadImageWindowController = [[SwitchHeadImageWindowController alloc] initWithWindowNibName:@"SwitchHeadImageWindowController"];
	
    //返回选择头像啊结果
    _headImage = [_switchHeadImageWindowController headImage:self];
    [_buttonHeadImage setImage:_headImage];
}


#pragma mark -
#pragma mark Register delegate

-(void) registerComplete:(BOOL)flag UserID:(NSUInteger)userid
{
    if(flag)
    {
        NSString *userIdInfo = [[NSString alloc] initWithFormat:NSLocalizedString(@"注册号码为%ld",nil),userid];
        NSAlert *alert = [[NSAlert alloc] init];
        [alert addButtonWithTitle:NSLocalizedString(@"确定", nil)];
        [alert setMessageText:NSLocalizedString(@"恭喜注册成功",nil)];
        [alert setInformativeText:userIdInfo];
        [alert setAlertStyle:NSWarningAlertStyle];
        [alert beginSheetModalForWindow:[NSApp mainWindow] modalDelegate:self didEndSelector:@selector(didCloseAlter:returnCode:contextInfo:) contextInfo:nil];
    }
    else
    {
        [self registerAlter:NSLocalizedString(@"请检查你的网络状况",nil) MesageText:NSLocalizedString(@"注册失败",nil)];
    }
}

@end
